<template>
  <div class="flex flex-col h-full">
    <SearchContainer show-export @search="getData" @reset="() => formRef.resetFields()" @export="exportRef.$el.querySelector('button').click()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange" label="挂号日期">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="jzsx" label="就诊事项" class="w-[260px] ml-3">
          <a-select v-model:value="formState.jzsx" placeholder="请选择就诊事项" allow-clear>
            <a-select-option v-for="(item, index) in dictOutList" :key="index" :value="item.dictOutpMatterId">{{ item.matterName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="客户来源" class="w-[260px]">
          <SelectCustomerSource v-model:value="formState.zjm" max-tag-count="responsive" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="flex-1">
      <a-space ref="exportRef" class="hidden">
        <BaseExport type="报表" name="推荐渠道统计(按登记)" code="MAP000444" is-show-time :search-params="formState">导出</BaseExport>
      </a-space>
      <BaseTable v-if="columns.length" :scroll="{ x: columns.length * 140 }" :loading="loading" :data-source="tableData" fill :columns="columns">
        <!-- <template #bodyCell="{ column, index }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
        </template> -->
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'

const exportRef = ref(null)
const formRef = ref(null)
const dictOutList = ref([])
const formState = ref({
  dateRange: [dayjs().subtract(20, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  jzsx: undefined,
  zjm: undefined
})
const columns = ref([])
const { loading, tableData, getData } = useTable({
  url: '/statistic/getPatientStatisticsByCustomerPath',
  formatParams: () => {
    return {
      ...formState.value,
      stdate: formState.value.dateRange?.[0],
      eddate: formState.value.dateRange?.[1],
      zjm: formState.value.zjm
    }
  },
  pagination: false,
  immediate: true,
  onSuccess: (res) => {
    if (res.length <= 0) return
    const idx = findObjectWithMostKeyValuePairs(res)
    columns.value = Object.keys(res[idx]).map((item) => {
      const align = (typeof res[0][item] === 'number' && item !== '序号') || item.indexOf('金额') > -1 || item.indexOf('费') > -1 ? 'right' : 'left'
      const width = item === '序号' ? 60 : ''
      const customRender = item.indexOf('金额') > -1 || item.indexOf('费') > -1 ? ({ text }) => formatAmount(text, 2) : null
      return { title: item, dataIndex: item, customRender, align, ellipsis: true, width }
    })
  }
})

// 获取就诊事项
const getDictOutList = async () => {
  const res = await post('/outp/mzdj/getDictOutpMatterNumberList')
  if (res.code !== 0) return message.error(res.msg)
  dictOutList.value = res.data
}
const findObjectWithMostKeyValuePairs = (arr) => {
  const arrObj = []
  arr.forEach((item, index) => {
    arrObj.push({ num: Object.keys(item).length, idx: index })
  })
  const max = arrObj.sort((a, b) => b.num - a.num)[0].idx // 40
  return max // 返回键值对最多的对象
}
onMounted(() => {
  getDictOutList()
})
</script>
